const app=getApp();
Page({
  data: {
    app:app,
    types:[],//分类，包含商品分页数据 paging:{data:[商品数据],maxNum,num,nowPage,upPage,downPage,maxPage}
    activeTab:0,//所选分类index
  },
  onLoad(query) {
    query.tid=query.tid?query.tid:-1;
    this.setData({
      query:query,//{tid:分类id}
    });
    this.init();
  },
  init(){
    app.myRequest({
      params:{
        act:"zfb-shops-types",
      },
      success:json=>{
        if(json.status !== "ok"){return;}
        json.data.unshift({id:-1,tit:"推荐"});
        let index=0;
        console.log("this.data.query",this.data.query)
        json.data = json.data.map((item,i)=>{
          if(item.id==this.data.query.tid){
            index=i;
          }
          return Object.assign({},item,{
            title:item.tit,
            paging:{data:[]},
            anchor:item.id
          });
        });
        console.log("types:json.data",json.data)
        this.setData({types:json.data});
        this.handleTabClick(index);
      }
    });
  },
  paging(index,p){
    const types = this.data.types;
    console.log("sdfsfsd")
    app.myRequest({
      params:{
        act:"zfb-shops-product-paging",
        p:p,
        tid:types[index].id
      },
      success:json=>{
        if(json.status !== "ok"){return;}
        json.data.data=json.data.data.map(item=>Object.assign({},item,{price:app.fenToYuan(item.price),image:app.getImageUrl(item.image)}));

        json.data.data = types[index].paging.data.concat(json.data.data);
        
        types[index].paging = json.data;

        this.setData({types:types});
        console.log("types[index].paging.data",types[index].paging.data);
      }
    })
  },
  handleTabClick(index){
    this.setData({
     activeTab:index
    });
    console.log("this.data.types[index]",this.data.types[index]);
    // return;
    if(!this.data.types[index].paging.data.length){
      this.paging(index,1);
    }
  },
  // 搜索
  onConfirm(){

  },
  // 跳转
  navigateTo(e){
    // my.navigateTo(e.currentTarget.dataset);
    app.navigateTo(e);
  }
});